﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using QzEbook.BaseView;
using System.Text;
using System.Collections.Generic;
using QzEbook.Common.Delegate;
using QzEbook.Common;
using QzEbook.Admin;
using QzEbook.View;

public partial class user : BasePage
{
    private WebMaster master;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindSitemap();
            BindData();
        }
    }

    private void BindSitemap()
    {
        master = this.Master as WebMaster;
        string name = "所有用户";
        string path = Request.Url.AbsolutePath;
        QzSiteMapNode node = new QzSiteMapNode(path, name, name);
        master.sitemap.addChild(node);
        string UserID=Request["Uid"] as string;

        //是否是在祥细用户上;
        if (String.IsNullOrEmpty(UserID))return;
        name = "用户";
        path = string.Format("?Uid={0}", UserID);
        node = new QzSiteMapNode(path, name, name);
        master.sitemap.addChild(node);
    }

    private void BindData()
    {
        Control adBarRef = this.Master.FindControl("adBar");
        Control contentBody = this.Master.FindControl("ContenBody");

        SessionUser userSession = Session[SessionManager.LOGINUSER] as SessionUser;
        string UserID=Request["Uid"] as string;

        //没有具体用户时,显示所有用户列表;
        if (String.IsNullOrEmpty(UserID)) {
            Control userListControl = this.Page.LoadControl("~/module/userList.ascx");
            contentBody.Controls.Add(userListControl);
            return;
        }

        string type=Request["type"] as string;

        
      


        if (!string.IsNullOrEmpty(type))
        {
            module_viewBooks viewer=null;
            string title = string.Empty;
            switch (type)
            {
                case "private" :
                    if (userSession != null && !String.IsNullOrEmpty(UserID) && userSession.Uid == int.Parse(UserID))
                    {
                        title = "私有列表";
                        viewer=addChildToControl(contentBody,title , "BIsVisible=0") as module_viewBooks;
                    }
                    break;
                case "public" :
                    title= "公开列表";
                    viewer = addChildToControl(contentBody,title, "BPrice=0 and BIsVisible<>0") as module_viewBooks;
                    break;
                case "shopping" :
                    title = "书店列表";
                    viewer = addChildToControl(contentBody, title, "BPrice<>0 and BIsVisible<>0") as module_viewBooks;
                    break;
                default :
                    break;
            }

            if (viewer != null)
            {
                master.sitemap.addChild(new QzSiteMapNode(Request.Url.PathAndQuery, title,title));

                viewer.PageSize = 12;
                viewer.ShowPagination = true;
            }

            return;
        }

        string path = Request.Url.Query;
        //具体用户;
        string titleString = String.Format("<a href='{0}&type=private'>私有电子书</a><sup>Beta</sup>", path);
        if (userSession != null && !String.IsNullOrEmpty(UserID) && userSession.Uid == int.Parse(UserID))
        {
            
            addChildToControl(contentBody,titleString, "BIsVisible=0");
            //addChildToControl(contentBody, "审核中的书<sup>Beta</sup>", "BAuditing<2");
        }
        titleString = string.Format("<a href='{0}&type=shopping'>书店热门</a>",path);
        addChildToControl(contentBody, titleString, "BPrice<>0 and BIsVisible<>0");
        titleString=string.Format("<a href='{0}&type=public'>共享热书</a>",path);
        addChildToControl(contentBody,titleString , "BPrice=0 and BIsVisible<>0");
        //addChildToControl(contentBody, "我的藏品书<sup>Beta</sup>", string.Empty);
        
    }

    /// <summary>
    /// 加入用户电子书;
    /// </summary>
    /// <param name="parentControl"></param>
    /// <param name="title"></param>
    /// <param name="filter"></param>
    /// <returns></returns>
    private Control addChildToControl(Control parentControl,string title,string filter)
    {
        Control viewerControl = this.Page.LoadControl("~/module/viewBooks.ascx");
        module_viewBooks viewer=viewerControl as module_viewBooks;
        viewer.PageSize = 6;
        viewer.DefaultFilterString = string.Empty;
        viewer.BindDataFinished = new ViewBookLoadedDelegate(viewerDataFinished);
        if(!String.IsNullOrEmpty(filter)){
            viewer.FilterAt = filter;
        }
        Literal literal = new Literal();
        literal.Text =string.Format("<h3>{0}</h3>",title,filter);
        viewer.TitleLiteral = literal;
        parentControl.Controls.Add(viewer);
        return viewer;
    }

    private void viewerDataFinished(Control send)
    {
        module_viewBooks views = send as module_viewBooks;
        if (views.IsEmpty)
        {
            views.Visible = false;
        }
    }

}
